﻿Imports System.Data.OleDb

Public Class parametre
    Dim categories As New ArrayList
    Dim options As New ArrayList



    Dim dbCatModif As cls_taco
    Dim dbOptionsModif As cls_taco

    Private Sub parametre_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Dispose()
    End Sub

    'exemple de parametre
    'dateParam|	parametre|	valeur | champ correspondant dans le formulaire
    '2010-01-01	coutRetardAutreJour	100 frais pour les autres journées
    '2010-01-01	coutRetardJour1	100 frais pour la première journée
    '2010-01-01	dureeListeNoire	180 durée liste noire
    '2010-01-01	jourPerteDepot	1	après x jours (perte de depot apres x jours)
    '2010-01-01	nbFauteReservoirBlackList	2 nombre de fautes tolérés réservoir non plein
    '2010-01-01	nbMentionDommagePourRefus	3 nombre d'accients tolérés
    '2010-01-01	nbRetardBlackList	1 nombre de fautes tolérés (retard remise)
    '2010-01-01	prcDepotPerduRetard	1 pourcentage de dépot perdu
    '2010-01-01	surchargeReservoir	75 cout pour la surcharge du reservoir
    '2010-01-01	jourExemptAccident	75 pas d'accident depuis
    '2010-01-01	tps	0.05 tps (taxes)
    '2010-01-01	tvq	0.075 tvq (taxes)

    Private Sub parametre_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        panCatActif.Enabled = False
        remplirNomCategorie()
        remplirNomOptions()
        remplirinfo()


    End Sub

    Sub remplirNomCategorie()

        'instancie un objet pour chaque catégorie de la base de données
        Dim bdCategories As New cls_GestionBD("taco.mdb")
        bdCategories.remplirDataSet("SELECT * FROM tblCategories", "tblCategories")

        lstCats.Items.Clear()
        categories.Clear()

        For Each row As DataRow In bdCategories.dsBd.Tables("tblCategories").Rows
            categories.Add(New clsCategorie(row))
        Next

        'ajoute chaque catégorie dans la liste
        For Each categorie As clsCategorie In categories
            lstCats.Items.Add(categorie.nomCategorie)
        Next

        ClearerLesChampsCategorie()

        btnAjouter.Enabled = True
    End Sub

    Sub remplirNomOptions()
        Dim bdOptions As New cls_GestionBD("taco.mdb")
        bdOptions.remplirDataSet("SELECT * FROM tblOptions", "tblOptions")

        lstOptions.Items.Clear()
        options.Clear()

        For Each row As DataRow In bdOptions.dsBd.Tables("tblOptions").Rows
            options.Add(New clsOptions(row))
        Next

        For Each opt As clsOptions In options
            lstOptions.Items.Add(opt.nom)
        Next
        btnAjouter2.Enabled = True
        ClearerLesChampsOptions()
    End Sub


    Public Sub remplirinfo()
        'créé la connection à la base de données
        txtDureListeNoire.Text = getParam("dureeListeNoire")
        txtFauteRetard.Text = getParam("nbRetardBlackList")
        numPrcDepotPerdu.Value = getParam("prcDepotPerduRetard") * 100
        txtJourPerteDepot.Text = getParam("jourPerteDepot")
        txtFraisJour1.Text = getParam("coutRetardJour1")
        txtFraisAutre.Text = getParam("coutRetardAutreJour")
        txtFautePlein.Text = getParam("nbFauteReservoirBlackList")
        txtCoutSur.Text = getParam("surchargeReservoir")
        txtAccTolere.Text = getParam("nbMentionDommagePourRefus")
        txtJoursAccident.Text = getParam("jourExemptAccident")
        txtTps.Text = getParam("tps")
        txtTvq.Text = getParam("tvq")




    End Sub



    Private Sub btnAnnuler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnnuler.Click
        Me.Close()
    End Sub

    Private Sub btnAppliquer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAppliquer.Click
        Dim dbParam = New cls_taco

        dbParam.defDataset("SELECT * from tblParametres where dateParam >= all(select dateParam from tblParametres) ", "tblParametres")

        ' Dim dateParamCourante As DateTime

        ajouterParam(dtParametre.Value, "dureeListeNoire", CSng(txtDureListeNoire.Text))
        ajouterParam(dtParametre.Value, "nbRetardBlackList", CSng(txtFauteRetard.Text))
        ajouterParam(dtParametre.Value, "prcDepotPerduRetard", CSng(numPrcDepotPerdu.Value) / 100)
        ajouterParam(dtParametre.Value, "jourPerteDepot", CSng(txtJourPerteDepot.Text))
        ajouterParam(dtParametre.Value, "coutRetardJour1", CSng(txtFraisJour1.Text))
        ajouterParam(dtParametre.Value, "coutRetardAutreJour", CSng(txtFraisAutre.Text))
        ajouterParam(dtParametre.Value, "nbFauteReservoirBlackList", CSng(txtFautePlein.Text))
        ajouterParam(dtParametre.Value, "surchargeReservoir", CSng(txtCoutSur.Text))
        ajouterParam(dtParametre.Value, "nbMentionDommagePourRefus", CSng(txtAccTolere.Text))
        ajouterParam(dtParametre.Value, "jourExemptAccident", CSng(txtJoursAccident.Text))
        ajouterParam(dtParametre.Value, "tps", CSng(txtTps.Text))
        ajouterParam(dtParametre.Value, "tvq", CSng(txtTvq.Text))



    End Sub

    Private Sub Label16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label16.Click

    End Sub

    Private Sub lstCats_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCats.SelectedIndexChanged

        For Each categorie As clsCategorie In categories

            If categorie.nomCategorie = lstCats.SelectedItem Then

                ChargerInfoCat(categorie)
                btnModifier.Enabled = True
                btnModifier.Text = "Modifier"
                btnSupprimer.Enabled = True
                panCatActif.Enabled = False
                btnAjouter.Text = "Ajouter"

            End If

        Next
    End Sub



    Public Sub ChargerInfoCat(ByVal categorie As clsCategorie)

        With categorie
            txtCat.Text = .nomCategorie
            numPrcDepot.Value = .prcVoitureDepot * 100
            txtMontantDepot.Text = .montantDepot
            TxtCoutSurcharge.Text = .coutSurchargeKm
            TxtCoutLocation.Text = .coutLocationJour
            TxtMontantAssurance.Text = .assurance
            txtKmAvSurcharge.Text = .kmAvantSurcharge
            txtCommentaireCat.Text = .commentaire
        End With


    End Sub

    Private Sub btnModifier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModifier.Click
        If btnModifier.Text = "Modifier" Then

            panCatActif.Enabled = True
            btnModifier.Text = "Enregistrer"
            btnSupprimer.Enabled = False
            btnAjouter.Enabled = False
        Else
            If champCategorieVide() = False Then
                txtCat.Text = firstLetterCap(txtCat.Text)
                If Not lstCats.Items.Contains(txtCat.Text) Or lstCats.SelectedItem = txtCat.Text Then

                    For Each categorie As clsCategorie In categories

                        If categorie.nomCategorie = lstCats.SelectedItem Then

                            With categorie
                                .nomCategorie = txtCat.Text
                                .prcVoitureDepot = numPrcDepot.Value / 100
                                .montantDepot = txtMontantDepot.Text
                                .coutSurchargeKm = TxtCoutSurcharge.Text()
                                .coutLocationJour = TxtCoutLocation.Text()
                                .assurance = TxtMontantAssurance.Text()
                                .kmAvantSurcharge = txtKmAvSurcharge.Text()
                                .commentaire = txtCommentaireCat.Text()
                            End With

                            categorie.enregistrer()
                            Exit For
                        End If

                    Next

                    btnModifier.Text = "Modifier"
                    panCatActif.Enabled = False
                    remplirNomCategorie()

                Else
                    MsgBox("La catégorie '" & txtCat.Text & "' existe déjà", MsgBoxStyle.Exclamation, "Impossible d'effectuer la modification")
                End If
            Else
                MsgBox("Tous les champs de la catégorie sont obligatoires! À part le champ commentaire", MsgBoxStyle.Exclamation, "Impossible de modifier la catégorie")
            End If
        End If
    End Sub

    Private Sub btnSupprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSupprimer.Click
        Dim noCat As Integer = getNoRowFromNom("nomCategorie", txtCat.Text, "noCategorie", "tblCategories")
        For Each categorie As clsCategorie In categories
            If categorie.nomCategorie = lstCats.SelectedItem Then
                If Not ObjHasNoIn(noCat, "noCategorie", "tblVehicules") Then
                    categorie.supprimer()
                    Exit For
                Else
                    MsgBox("Des véhicules enregistré dans la base de données font parti de cette catégorie (" & txtCat.Text & ") , veuillez auparavant supprimer les véhicules ayant cette catégorie", MsgBoxStyle.Exclamation, "Impossible d'effectuer la suppression")
                End If
            End If

        Next
        remplirNomCategorie()
    End Sub

    Public Function champCategorieVide()

        Return txtCat.Text = "" Or txtMontantDepot.Text = "" Or txtKmAvSurcharge.Text = "" Or TxtCoutSurcharge.Text = "" Or TxtCoutLocation.Text = "" Or TxtMontantAssurance.Text = ""
    End Function

    Public Sub ClearerLesChampsCategorie()
        txtCat.Text = Nothing
        numPrcDepot.Value = Nothing
        txtMontantDepot.Text = Nothing
        TxtCoutSurcharge.Text = Nothing
        TxtCoutLocation.Text = Nothing
        TxtMontantAssurance.Text = Nothing
        txtKmAvSurcharge.Text = Nothing
        txtCommentaireCat.Text = Nothing
        panCatActif.Enabled = False
        btnModifier.Enabled = False
        btnSupprimer.Enabled = False
        btnModifier.Text = "Modifier"

    End Sub
    Public Sub ClearerLesChampsOptions()
        txtNomOption.Text = Nothing
        txtDescriptionOption.Text = Nothing
        panOpt.Enabled = False
        btnModifier2.Enabled = False
        btnSupprimer2.Enabled = False
        btnModifier2.Text = "Modifier"

    End Sub

    Private Sub btnAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjouter.Click


        If btnAjouter.Text = "Ajouter" Then

            btnAjouter.Text = "Enregistrer"
            ClearerLesChampsCategorie()
            panCatActif.Enabled = True
        Else
            If champCategorieVide() = False Then
                txtCat.Text = firstLetterCap(txtCat.Text)
                If Not lstCats.Items.Contains(txtCat.Text) Then

                    Dim categorie As New clsCategorie()

                    With categorie
                        .nomCategorie = txtCat.Text
                        .prcVoitureDepot = numPrcDepot.Value / 100
                        .montantDepot = nvlReturn(txtMontantDepot.Text, 0)
                        .coutSurchargeKm = nvlReturn(TxtCoutSurcharge.Text, 0)
                        .coutLocationJour = nvlReturn(TxtCoutLocation.Text, 0)
                        .assurance = nvlReturn(TxtMontantAssurance.Text, 0)
                        .kmAvantSurcharge = nvlReturn(txtKmAvSurcharge.Text, 0)
                        .commentaire = nvlReturn(txtCommentaireCat.Text, "")
                    End With

                    categorie.enregistrer()
                    categories.Add(categorie)
                    remplirNomCategorie()
                    panCatActif.Enabled = False
                    btnAjouter.Text = "Ajouter"

                Else
                    MsgBox("La catégorie '" & txtCat.Text & "' existe déjà", MsgBoxStyle.Exclamation, "Impossible d'effectuer l'ajout")
                End If
            Else
                MsgBox("Tous les champs de la catégorie sont obligatoires! À part le champ commentaire", MsgBoxStyle.Exclamation, "Impossible d'ajouter la catégorie")
            End If
        End If
    End Sub

    Private Sub txtMontantDepot_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMontantDepot.KeyPress
        If Not IsNumeric(e.KeyChar) And e.KeyChar <> ControlChars.Back And e.KeyChar <> ControlChars.Tab Then
            e.Handled = True
        End If
    End Sub


    Private Sub txtKmAvSurcharge_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtKmAvSurcharge.KeyPress
        If Not IsNumeric(e.KeyChar) And e.KeyChar <> ControlChars.Back And e.KeyChar <> ControlChars.Tab Then
            e.Handled = True
        End If
    End Sub



    Private Sub TxtCoutSurcharge_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtCoutSurcharge.KeyPress
        If Not IsNumeric(e.KeyChar) And e.KeyChar <> ControlChars.Back And e.KeyChar <> ControlChars.Tab Then
            e.Handled = True
        End If
    End Sub



    Private Sub TxtCoutLocation_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtCoutLocation.KeyPress
        If Not IsNumeric(e.KeyChar) And e.KeyChar <> ControlChars.Back And e.KeyChar <> ControlChars.Tab Then
            e.Handled = True
        End If
    End Sub


    Private Sub TxtMontantAssurance_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtMontantAssurance.KeyPress
        If Not IsNumeric(e.KeyChar) And e.KeyChar <> ControlChars.Back And e.KeyChar <> ControlChars.Tab Then
            e.Handled = True
        End If
    End Sub


    Public Sub ChargerInfoOption(ByVal opt As clsOptions)
        With opt
            txtNomOption.Text = .nom
            txtDescriptionOption.Text = .description
        End With
    End Sub

    Private Sub lstOptions_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstOptions.SelectedIndexChanged

        For Each opt As clsOptions In options

            If opt.nom = lstOptions.SelectedItem Then

                ChargerInfoOption(opt)
                btnModifier2.Enabled = True
                btnModifier2.Text = "Modifier"
                btnSupprimer2.Enabled = True
                panOpt.Enabled = False
                btnAjouter2.Text = "Ajouter"

            End If

        Next
    End Sub

    Private Sub btnAjouter2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjouter2.Click

        If btnAjouter2.Text = "Ajouter" Then

            btnAjouter2.Text = "Enregistrer"
            ClearerLesChampsOptions()
            panOpt.Enabled = True
        Else
            If champOptionVide() = False Then
                txtNomOption.Text = firstLetterCap(txtNomOption.Text)
                If Not lstOptions.Items.Contains(txtNomOption.Text) Then
                    Dim opt As New clsOptions()

                    opt.nom = txtNomOption.Text
                    opt.description = txtDescriptionOption.Text

                    opt.enregistrer()
                    options.Add(opt)

                    btnAjouter2.Text = "Ajouter"
                    panOpt.Enabled = False
                    remplirNomOptions()
                Else
                    MsgBox("L'option '" & txtNomOption.Text & "' existe déjà", MsgBoxStyle.Exclamation, "Impossible d'effectuer l'ajout")
                End If
            Else
                MsgBox("Veuillez entrez un nom d'option!", MsgBoxStyle.Exclamation, "Impossible d'ajouter l'option")
            End If
        End If
    End Sub

    Private Sub btnSupprimer2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSupprimer2.Click

        For Each opt As clsOptions In options
            If opt.nom = lstOptions.SelectedItem Then
                If Not ObjHasNoIn(opt.numero, "noOption", "tblOptionsVehicules") Then
                    opt.supprimer()
                    remplirNomOptions()
                Else
                    MsgBox("Des véhicules enregistré dans la base de données ont cette option (" & opt.nom & ") , veuillez auparavant supprimer les véhicules ayant cete option", MsgBoxStyle.Exclamation, "Impossible d'effectuer la suppression")
                End If
                Exit For
            End If
        Next
    End Sub

    Private Sub btnModifier2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModifier2.Click
        If btnModifier2.Text = "Modifier" Then

            panOpt.Enabled = True
            btnModifier2.Text = "Enregistrer"
            btnSupprimer2.Enabled = False
            btnAjouter2.Enabled = False
        Else
            If champOptionVide() = False Then
                txtNomOption.Text = firstLetterCap(txtNomOption.Text)

                If Not lstOptions.Items.Contains(txtNomOption.Text) Or lstOptions.SelectedItem = txtNomOption.Text Then

                    For Each opt As clsOptions In options
                        If opt.nom = lstOptions.SelectedItem Then
                            opt.nom = txtNomOption.Text
                            opt.description = txtDescriptionOption.Text
                            opt.enregistrer()
                            Exit For
                        End If
                    Next

                    btnModifier2.Text = "Modifier"
                    panOpt.Enabled = False
                    remplirNomOptions()
                Else
                    MsgBox("L'option '" & txtNomOption.Text & "' existe déjà", MsgBoxStyle.Exclamation, "Impossible d'effectuer la modification")
                End If
            Else

                MsgBox("Veuillez entrez un nom d'option", MsgBoxStyle.Exclamation, "Impossible de modifier l'option")
            End If
        End If
    End Sub

    Function nvlReturn(ByVal obj As Object, ByVal returnValue As Object) As Object
        If IsDBNull(obj) Or obj = Nothing Then
            Return returnValue
        Else
            Return obj
        End If

        Return 0
    End Function
    Public Function champOptionVide()

        Return txtNomOption.Text = ""
    End Function

    Public Function getNoRowFromNom(ByVal nomChamp As String, ByVal valeurNom As String, ByVal nomNo As String, ByVal nomTable As String) As Object

        Dim maCommande As New OleDbCommand

        Dim db = New cls_taco
        Dim requete As String
        requete = "SELECT * from " & nomTable & " where " & nomChamp & "=@valeurNom"

        maCommande.Parameters.Add("@valeurNom", OleDbType.VarChar, 50).Value = valeurNom

        maCommande.CommandText = requete
        db.defDataset(maCommande, nomTable)

        If db.ds.tables(nomTable).rows.count > 0 Then
            Return nvl(db.ds.tables(nomTable).rows(0).Item(nomNo), "")

        End If
        Return 0
    End Function

    Public Function ObjHasNoIn(ByVal noUnique As Integer, ByVal nomNumero As String, ByVal tbl As String) As Boolean

        Dim dbCar = New cls_taco

        dbCar.defDataset("SELECT * from " & tbl & " where " & nomNumero & "=" & noUnique, tbl)

        If dbCar.ds.Tables(tbl).Rows.count > 0 Then
            Return True
        Else
            Return False
        End If

    End Function

    Public Sub ajouterCategorie(ByVal nomCategorie As String, ByVal prcVoitureDepot As Single, ByVal montantDepot As Single, ByVal coutSurchargeKm As Single, ByVal coutLocationJour As Single, ByVal assurance As Single, ByVal kmAvantSurcharge As Single, ByVal commentaire As String)
        Dim lNewRow As DataRow
        Dim db As New cls_taco

        db.defDataset("SELECT * from tblCategories", "tblCategories")
        lNewRow = db.ds.Tables("tblCategories").NewRow

        lNewRow("noCategorie") = createNewNo("noCategorie", "tblCategories")
        lNewRow("nomCategorie") = nomCategorie
        lNewRow("prcVoitureDepot") = nvlReturn(prcVoitureDepot, DBNull.Value)
        lNewRow("montantDepot") = nvlReturn(montantDepot, DBNull.Value)
        lNewRow("coutSurchargeKm") = nvlReturn(coutSurchargeKm, DBNull.Value)
        lNewRow("coutLocationJour") = nvlReturn(coutLocationJour, DBNull.Value)
        lNewRow("assurance") = nvlReturn(assurance, DBNull.Value)
        lNewRow("kmAvantSurcharge") = nvlReturn(kmAvantSurcharge, DBNull.Value)
        lNewRow("commentaire") = nvlReturn(commentaire, DBNull.Value)

        db.ds.Tables.Item(0).Rows.Add(lNewRow)
        db.update("tblCategories")

    End Sub

    Public Sub ajouterOption(ByVal nomOption As String, ByVal description As String)
        Dim lNewRow As DataRow
        Dim db As New cls_taco

        db.defDataset("SELECT * from tblOptions", "tblOptions")
        lNewRow = db.ds.Tables("tblOptions").NewRow

        lNewRow("noOption") = createNewNo("noOption", "tblOptions")
        lNewRow("nomOption") = nomOption
        lNewRow("descriptionOption") = description

        db.ds.Tables.Item(0).Rows.Add(lNewRow)
        db.update("tblOptions")

    End Sub




    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        btnAppliquer.PerformClick()
        Me.Close()
    End Sub
End Class